/**
 * yccrmserver-core
 * @(#) BaseRestServiceImpl.java 2011 2011-12-26 下午12:34:41
 * 
 * Copyright 2011 Chengdu Sunsharp Science & Techniques, Inc. All rights reserved.
 */
package com.huyanqi.core.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.huyanqi.core.BaseDomainObject;
import com.huyanqi.core.RetdCodeType;
import com.huyanqi.core.RetdObj;
import com.huyanqi.core.dao.BaseDAO;
import com.huyanqi.core.service.BaseRestService;

/**
 * <p>Title: BaseRestServiceImpl.java</p>
 * 
 * <p>Description: Rest Service 基类实现</p>
 * 
 * <p>Datetime: 2011-12-26 下午12:34:41</p>
 *
 * @company 成都映潮科技有限公司
 * 
 * @author <a href="mailto:liwei@sunsharp.cn">p.k.ripper</a>
 *
 */
@Transactional
public abstract class BaseRestServiceImpl<T extends BaseDomainObject,PK extends Serializable,EntityDAO extends BaseDAO<T,PK>> implements BaseRestService<T,PK>{
	protected Logger log = Logger.getLogger(this.getClass());
	
	protected EntityDAO entityDao;
			

	public EntityDAO getEntityDao() {
		return entityDao;
	}
	//由子类实现
	public abstract void setEntityDao(EntityDAO entityDao);
	@Override
	public RetdObj<T> save(T entity) {
		RetdObj<T> rt = new RetdObj<T>();
		try {
			T t = this.entityDao.saveEntity(entity);
			rt.setRealObj(t);
			rt.setCode(RetdCodeType.PASS_OK);			
			return rt;
		} catch (Exception e) {
			log.error(e.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}

	@Override
	public RetdObj<T> update(T entity) {
		RetdObj<T> rt = new RetdObj<T>();
		try {
			T t= this.entityDao.updateEntity(entity);
			rt.setRealObj(t);
			rt.setCode(RetdCodeType.PASS_OK);
			return rt;
		} catch (Exception e) {
			log.error(e.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}

	@Override
	public RetdObj<T> delete(PK id) {
		RetdObj<T> rt=new RetdObj<T>();
		try{
			this.entityDao.deleteEntity(id);
			rt.setCode(RetdCodeType.PASS_OK);
			return rt;
		}catch(Exception e){
			log.error(e.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}
	/**
	 * 按对象删除
	 * 
	 * @param id
	 */
	public RetdObj<T> delete(T entity){
		RetdObj<T> rt=new RetdObj<T>();
		try{
			this.entityDao.deleteEntity(entity);
			rt.setCode(RetdCodeType.PASS_OK);
			return rt;
		}catch(Exception e){
			log.error(e.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}
	
	@Override
	public RetdObj<T> findById(PK id) {
		RetdObj<T> rt = new RetdObj<T>();
		try {
			T entity = this.entityDao.findEntityById(id);
			if (entity == null) {
				rt.setCode(RetdCodeType.PASS_NODATA);
				return rt;
			} else {
				rt.setCode(RetdCodeType.PASS_OK);
				rt.setRealObj(entity);
				return rt;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}

	@Override
	public RetdObj<List<T>> findByIds(Collection<PK> ids) {
		RetdObj<List<T>> rt = new RetdObj<List<T>>();
		try {
			List<T> entities = this.entityDao.findByIds(ids);
			if (entities == null || entities.size() < 1) {
				rt.setCode(RetdCodeType.PASS_NODATA);
				return rt;
			} else {
				rt.setCode(RetdCodeType.PASS_OK);
				rt.setRealObj(entities);
				return rt;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}
	public RetdObj<List<T>> findAllByNameParams(String queryName, Object... parameters){
		RetdObj<List<T>> rt = new RetdObj<List<T>>();
		try {
			List<T> entities = this.entityDao.executeQueryByName(queryName, -1, -1, parameters);
			if (entities == null || entities.size() < 1) {
				rt.setCode(RetdCodeType.PASS_NODATA);
				return rt;
			} else {
				rt.setCode(RetdCodeType.PASS_OK);
				rt.setRealObj(entities);
				return rt;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}
	
	/**
	 * 根据属性查询
	 * @param propertyName
	 * @param value
	 * @return RetdObj<List<Entity>>
	 */
	public RetdObj<List<T>> findByProperty(String propertyName, Object value){
		RetdObj<List<T>> rt = new RetdObj<List<T>>();
		try {
			List<T> entities = this.entityDao.findByProperty(propertyName, value);
			if (entities == null || entities.size() < 1) {
				rt.setCode(RetdCodeType.PASS_NODATA);
				return rt;
			} else {
				rt.setCode(RetdCodeType.PASS_OK);
				rt.setRealObj(entities);
				return rt;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}
	public RetdObj<List<T>> findByProperty(Map<String, Object> values){
		RetdObj<List<T>> rt = new RetdObj<List<T>>();
		try {
			List<T> entities = this.entityDao.findByProperty(values);
			if (entities == null || entities.size() < 1) {
				rt.setCode(RetdCodeType.PASS_NODATA);
				return rt;
			} else {
				rt.setCode(RetdCodeType.PASS_OK);
				rt.setRealObj(entities);
				return rt;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage());
			rt.setCode(RetdCodeType.EX_APP);
			return rt;
		}
	}

}
